package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XuechengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachPlanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Resource
    private TeachplanMapper teachplanMapper;
    @Resource
    private TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {
        Long id = saveTeachplanDto.getId();
        if(id==null){
            //新增大小章节
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            Long parentId = saveTeachplanDto.getParentid();
            Long courseId = saveTeachplanDto.getCourseId();
            teachplan.setOrderby(getTeachplanCount(parentId,courseId)+1);
            teachplan.setCreateDate(LocalDateTime.now());
            teachplanMapper.insert(teachplan);
        }else {
            //修改
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    private int getTeachplanCount(Long parentId, Long courseId) {
        LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid,parentId);
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }

    @Transactional
    @Override
    public void deleteTeachplan(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        if(teachplan.getParentid().equals(0L)){
            //如果该章节是大章节
            LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getParentid,id);
            int i = teachplanMapper.selectCount(queryWrapper);
            if(i>0){//该大章节下还有小章节
                throw new XuechengPlusException("课程计划信息还有子级信息，无法操作");
            }
            int deleteById = teachplanMapper.deleteById(id);
            if(deleteById<=0){
                throw new XuechengPlusException("删除失败");
            }
        }else {
            //如果该章节为小章节
            int i = teachplanMapper.deleteById(id);
            if(i<=0){
                throw new XuechengPlusException("删除失败");
            }
            //将关联的课程媒体信息删除
            LambdaQueryWrapper<TeachplanMedia> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(TeachplanMedia::getTeachplanId,id);
            Integer i1 = teachplanMediaMapper.selectCount(queryWrapper);
            if(i1<=0)
                return;
            int delete = teachplanMediaMapper.delete(queryWrapper);
            if(delete<=0){
                throw new XuechengPlusException("删除失败");
            }
        }
    }

    @Transactional
    @Override
    public void moveTeachplan(Long id, int move) {//move:移动参数，1上移，2下移
        Teachplan teachplan = teachplanMapper.selectById(id);
        Integer orderby = teachplan.getOrderby();
        LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId());
        queryWrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        int i = teachplanMapper.selectCount(queryWrapper);
        if(i==1){//同类章节就一个，不用动
            return;
        }
        if(move==1){
            if(orderby==1)//已经是第一位了，不用动
                return;
            //交换该章节和上一章节的顺序
            queryWrapper.eq(Teachplan::getOrderby,orderby-1);
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);//查出该章节的上一个章节
            teachplans.get(0).setOrderby(orderby);//将上一章节的排序字段值设为当前章节的orderby
            teachplanMapper.updateById(teachplans.get(0));
            teachplan.setOrderby(orderby-1);//将该章节上移一位
            teachplanMapper.updateById(teachplan);
        }else {
            //交换该章节和下一章节的顺序
            queryWrapper.eq(Teachplan::getOrderby,orderby+1);
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);//查出该章节的下一个章节
            if(teachplans.size()==0){//已经在最下面，不用动
                return;
            }
            teachplans.get(0).setOrderby(orderby);//将下一章节的排序字段值设为当前章节的orderby
            teachplanMapper.updateById(teachplans.get(0));
            teachplan.setOrderby(orderby+1);//将该章节下移一位
            teachplanMapper.updateById(teachplan);
        }
    }

    @Override
    public void associationMedia(BindTeachPlanMediaDto bindTeachPlanMediaDto) {
        //用课程计划id找到课程id
        Teachplan teachplan = teachplanMapper.selectById(bindTeachPlanMediaDto.getTeachplanId());
        if(teachplan==null){
            XuechengPlusException.cast("课程计划不存在");
        }
        //删除原有绑定
        int delete = teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId, bindTeachPlanMediaDto.getTeachplanId()));

        //向表中插入一条绑定
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachPlanMediaDto,teachplanMedia);
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setMediaFilename(bindTeachPlanMediaDto.getFileName());
        teachplanMediaMapper.insert(teachplanMedia);
    }

    @Override
    public void unbindMedia(String teachPlanId, String mediaId) {
        int delete = teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId, teachPlanId).eq(TeachplanMedia::getMediaId, mediaId));
    }
}
